// packageG/mycomponent/courseItem/courseList.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  options: {
    multipleSlots: true, // 复数插槽: 是
    addGlobalClass: true,
  },
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
    page_id: {
      type: null,
    },
    bottomShowBol: {
      type: null,
    },
    bottomShowBolIndex: {
      type: null,
    },
    index: {
      type: null,
    },
    btnFlag: {
      type: null
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    courseList:[],
    isLoadMore: true, //true 可以加载更多
    isLoadMoreSet:false,
    page: 1,
    total_page: 0,
    arrIndex: 1, //组件加载下一页
    per_page:5, //每页多少条数据
    courseUnList:[],
    courseListData:[], //总数据处理
  },
  lifetimes: {
    created() {
    },
    attached: function () {
      console.log(this.data);
    }
  },
  observers: {
    datas: function (data) {
      console.log(data);
      if (this.data.totalPage <= this.data.page) {
        this.setData({
          isLoadMore: false,
          allLoaded: true,
        });
      }
    },
    bottomShowBol: function (flag) {
      if (flag && this.data.bottomShowBolIndex == this.data.index && this.data.isLoadMore) {
        this.getMoreData();
      } else {
        this.getMoreDataAll();
      }
    },
  },
  ready() {
    console.log(this.data);
    if (this.data.datas.list.data) {
      this.setData({
        total_page: this.data.datas.list.last_page,
      });
      if (this.data.total_page <= this.data.page) {
        this.setData({
          isLoadMore: false,
        });
      }
      this.setData({
        courseList: this.data.datas.list.data,
      });
    } else {
      if (this.data.datas.list.length <= 0) {
        this.setData({
          isLoadMore: false,
        });
      }
      this.setData({
        courseList: this.data.datas.list,
      });
    }

    if(this.data.datas.get_style == "2") {
      this.setData({
        isLoadMore: false,
        courseUnList: this.data.datas.list,
        courseList:this.data.datas.list.slice(0, 5),
        isLoadMore:false,
        isLoadMoreSet:true
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    courseAll() {
      wx.navigateTo({ 
        url: '/packageF/course/courseAll/courseList', 
      })
    },
    getMoreDataAll() {
      if (this.data.courseUnList.slice(this.data.per_page * this.data.arrIndex).length > 0) {
        this.data.courseList = this.data.courseList.concat(this.data.courseUnList.slice(this.data.per_page * this.data.arrIndex, this.data.per_page * (this.data.arrIndex + 1))); //数组拼接
        // this.data.arrIndex = this.data.arrIndex + 1;
        this.setData({
          courseList: this.data.courseList,
          arrIndex:this.data.arrIndex + 1
        })
        if(this.data.courseUnList.length === this.data.courseList.length) {
          this.setData({
            isLoadMoreSet: false,
          })
        }
      }
    },
    // 获取更多数据
    getMoreData() {
      let that = this;
      that.setData({
        isLoadMore: false,
      });
      if (
        that.data.page >= that.data.total_page 
      ) {
        return;
      } else {
        that.setData({
          page: that.data.page + 1,
        });
        let urlStr = app.getNetAddresss("home-page.get-decorate-page");
        urlStr += "&page=" + that.data.page;
        urlStr += "&decorate_id=" + that.data.page_id;
        urlStr += "&component_id=" + that.data.component_id;
        urlStr += "&component_key=U_courseSupply";
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              if (that.data.page < that.data.total_page) {
                that.setData({
                  isLoadMore: true,
                });
              }
              let data = that.data.courseList.concat(res.data.data);
              that.setData({
                courseList: data,
              });
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {
            console.log(res);
          },
        });
      }
    },
  },
  
})
